#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

/**
 * mat指针方式遍历
 */
int main(){
    cv::Mat src= cv::imread("../resource/wukong.jpg");
    if (src.empty()) {
        cout << "could not load image...\n" << endl;
        return -1;
    }

    int rows = src.rows;
    int cols = src.cols;
    int channels = src.channels();

    //从地址存储上看，数据是顺序存储的，并且是一位数据的形式，所以只需要将指针向下推进即可访问下一个存储单元
    for (int i = 0; i < rows; i++) {
        uchar* current_row = src.ptr<uchar>(i); //当前行指针
        for (int j = 0; j < cols; j++) {
            if (channels==1) {
                int pv = *current_row; //当前行数值
                *current_row++ = 255 - pv; //给当前位置赋值，并将指针指向下一个位置
            } else if (channels==3){
                *current_row++= 255 - *current_row;
                *current_row++= 255 - *current_row;
                *current_row++= 255 - *current_row;
            }
        }
    }

    cv::imshow("src", src);
    cv::waitKey(0);
    cv::destroyAllWindows();

    return 0;
}